package com.chianghao.rock.system.service.route;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.chianghao.core.exception.HaoException;
import com.chianghao.core.frame.service.BaseService;
import com.chianghao.core.mybatis.sql.SqlOrder;
import com.chianghao.core.mybatis.sql.db_enum.SqlOrderType;
import com.chianghao.rock.Constant;
import com.chianghao.rock.system.dao.route.RouteMapper;
import com.chianghao.rock.system.entity.Route;

@Service
public class RouteService extends BaseService<RouteMapper, Route> {

	
	
	
	
	private boolean isExistRouteCode(String code) {
		int count = this.mapper.queryRouteCodeCount(code);
		return count==0?false:true;
	}
	
	private boolean isExistRouteCodeExcludedId(String code,long id) {
		int count = this.mapper.queryRouteCodeCountExcludedId(code,id);
		return count==0?false:true;
	}
	
	private boolean isExistRouteRoute(String route) {
		int count = this.mapper.queryRouteRouteCount(route);
		return count==0?false:true;
	}
	
	private boolean isExistRouteRouteExcludedId(String route,long id) {
		int count = this.mapper.queryRouteRouteCountExcludedId(route,id);
		return count==0?false:true;
	}
	
	private void checkRouteFormat(Long parentId,String route) {
		if(parentId!=null&&parentId!=Constant.rootId) {
			Route parent  = this.queryOneById(parentId);
			if(parent==null) {
				throw new HaoException("error:parent.not.null");
			}
			String[] parentRouteArray = parent.getRoute().split("/");
			String[] routeArray       = route.split("/");
			if(parentRouteArray.length+1!=routeArray.length||parentRouteArray.length>=routeArray.length) {
				throw new HaoException("error:route,incorrect.format");
			}
			for(int i=0;i<parentRouteArray.length;i++) {
				if(!parentRouteArray[i].equals(routeArray[i])) {
					throw new HaoException("error:route,incorrect.format");
				}
			}
		}
	}
	
	/**
	 * 创建路由
	 * @param route
	 */
	public void doCreate(Route route) {
		//判断code是否重复
		if(isExistRouteCode(route.getCode())){
			throw new HaoException("error:code,not.repeatable");
		}
		//判断路由是否重复
		if(isExistRouteRoute(route.getRoute())){
			throw new HaoException("error:route,not.repeatable");
		}
		// checkRouteFormat(route.getParentId(),route.getRoute());

		//写入数据库
		this.insert(route);
	}

	
	
	
	/**
	 * 修改路由
	 * @param route
	 */
	public void modify(Route route) {
		route.setParentId(null);
		//判断code是否重复
		if(StringUtils.isNotEmpty(route.getCode())&&isExistRouteCodeExcludedId(route.getCode(),route.getId())){
			throw new HaoException("error:code,not.repeatable");
		}
		//判断路由是否重复
		if(StringUtils.isNotEmpty(route.getRoute())&&isExistRouteRouteExcludedId(route.getRoute(),route.getId())){
			throw new HaoException("error:route,not.repeatable");
		}
		
		if(StringUtils.isNotEmpty(route.getRoute())) {
			checkRouteFormat(route.getParentId(),route.getRoute());
		}

		//写入数据库
		this.updateById(route, route.getId());
	}
	
	
	/**
	 * 删除路由相关数据
	 * @param id
	 */
	public void delete(Long id) {
		Route route = this.queryOneById(id);
		//删除路由
		if(route.getType().intValue()==0) {
			this.deleteRoute(id);
		}
		//删除按钮连接 或者删除 api
		if(route.getType().intValue()==1||route.getType().intValue()==2) {
			deleteButtonOrApi(id);
		}
	}
	
	
	/***
	 * 删除连接或者api
	 * @param id
	 */
	public void deleteButtonOrApi(long id) {
		this.deleteById(id);
	}
	
	/**
	 * 查新子节点
	 * @param parentId
	 * @return
	 */
	public List<Route> queryChildren(long parentId){
		Route where = new Route();
		where.setParentId(parentId);
		return this.mapper.queryListByEntity(where, new SqlOrder().add("sort",SqlOrderType.asc));
	}
	
	/**
	 * 第归删除子节点
	 * @param id
	 */
	public void deleteRoute(Long id) {
		List<Route> children = queryChildren(id);
		if(children!=null&&!children.isEmpty()) {
			for(Route r:children) {
				deleteRoute(r.getId());
			}
		}
		this.deleteById(id);
	}


	/**
	 * 添加按钮连接
	 * @param route
	 */
	public void doCreateButtonOrApi(Route route) {
		if(route.getParentId().longValue()==Constant.rootId) {
			throw new HaoException("error:can.not.build.button.on.root.route");
		}
		//检查code是否重复
		if(isExistRouteCode(route.getCode())){
			throw new HaoException("error:code,not.repeatable");
		}
		this.insert(route);
	}
	
	/**
	 * 修改连接按钮或者api接口
	 * @param route
	 */
	public void modiyButtonOrApi(Route route) {
		route.setParentId(null);
		if(StringUtils.isNotEmpty(route.getCode())&&isExistRouteCodeExcludedId(route.getCode(),route.getId())){
			throw new HaoException("error:code,not.repeatable");
		}
		this.updateById(route, route.getId());
	}

	public List<Route> queryChildrenEqualType(Long parentId,int type) {
		Route where = new Route();
		where.setParentId(parentId);
		where.setType(type);
		return this.mapper.queryListByEntity(where, new SqlOrder().add("sort",SqlOrderType.asc));
	}

	/**
	 * 查新全部的路由
	 * @return
	 */
	public List<Route> queryAllRoute() {
		Route where = new Route();
		where.setType(0);
		return this.mapper.queryListByEntity(where, new SqlOrder().add("sort",SqlOrderType.asc));
	}

	public List<Route> queryAllApi() {
		Route where = new Route();
		where.setType(2);
		return  this.mapper.queryListByEntity(where,null);
	}

	
	

}